﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Admin
{
    //this page allows the administrater to choose a customer from the data grid for editing.
    public partial class ChooseCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //function redirects the administrater back to his homepage when he presses the "back" button.
        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }

        //function for redirecting the administrater to the chosen clients edit page. 
        protected void DataGridProjects_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            // Retrieving the client id of the client chosen from the datagrid
            int clientId = Int32.Parse(e.Item.Cells[0].Text);

            // Determine the command
            switch (e.CommandName)
            {
                case ("Watch"):
                {
                    Response.Redirect("ShowCustomer.aspx?customerId=" + clientId);
                    break;
                }
                case ("Edit"):
                {
                    Response.Redirect("EditCustomer.aspx?customerId=" + clientId);
                    break;
                }
                case ("Delete"):
                {
                    Queries queries = new Queries();
                    queries.ClientToInactive(clientId);
                    Response.Redirect("~/Admin/AdminMenu.aspx");
                    break;
                }
            }
        }

        //function for handeling the page indexing
        protected void DataGridProjects_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            //saving the index chosen by the user.
            int index = e.NewPageIndex;
            //updating the current page for the data grid to display.
            DataGridProjects.CurrentPageIndex = index;
            //binding the data.
            DataGridProjects.DataBind();
        }

        /// <summary>
        /// Occurs when the user filters or unfilters the results by
        /// the customer's name, and updates results.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            // Filter
            if ((btnFilter.Text == "סנן") && (!String.IsNullOrEmpty(txtCustomerName.Text)))
            {
                SqlDataSource1.FilterExpression = 
                    String.Format("Name LIKE '%{0}%'", txtCustomerName.Text);
                btnFilter.Text = "בטל סינון";
            }
            // Unfilter
            else
            {
                SqlDataSource1.FilterExpression = null;
                btnFilter.Text = "סנן";
            }
        }
    }
}